{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [3. Array](03.00-Array.ipynb) | [Contents](Index.ipynb) | [Useful Attributes and Methods](03.02-Useful-Attributes-and-Methods.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.1生成连续数组\n",
    "\n",
    "通常我们需要向量遵循简单的顺序，例如，包含元素[10,11,12,13]或[5,10,15,20]或[1.0,1.2,1.4,1.6,1.8,2.0]的向量。我们看到，在这些向量中，元素遵循一些简单的顺序，所以如果有简单的方法来定义这些向量就更好了。一些创建这些向量的方法如下：\n",
    "\n",
    "### 3.1.1 linspace\n",
    "\n",
    "如果我们对生成向量感兴趣，向量的元素有着统一间距，知道上下限以及元素数量，那么这种情况下`linspace`是首要的选择:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.linspace(0,2,9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为`linspace`位于`numpy`库中，所以我们首先需要导入`numpy`库，并给它一个缩写名。之后我们调用`linspace`用下限、上限和要生成的元素数量。在这个例子中，0是下限，2是上限，9是元素个数。让我们生成更多的向量以更好的理解这个函数，这次我们以0为下限，2π为上限，元素个数设为100."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(0,2*np.pi,100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "元素个数默认为50，所以如果我们没有指定元素个数，那么我们会得到50个等间距的元素。我们可以使用`len`函数获得任意数组的长度。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = np.linspace(0,1)\n",
    "len(foo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1.2 arange\n",
    "\n",
    "再假设我们想要生成一个有着统一元素间距的向量，但这次我们不知道具体的元素数量，我们只是知道元素间的增量(间距)。在这种情况下，`arrange`可以被使用。`arrange`同样需要上下限。在接下来的例子中，我们将要生成的向量下限10，上限为30，增量为30。所以从`linspace`中得来的知识，我们可以这么做："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 15, 20, 25])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange( 10, 30, 5 )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "哦！发生了什么？为什么Python没有打印30。因为`arrange`函数不包含元素中的第二个参数。所以我们想要打印到30，我们会这样做。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 15, 20, 25, 30])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange( 10, 31, 5 )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这次我们得到了需要的输出。`arrange`同样可以取浮点数增量。让我们生成一个下限为0，上限为2，增量为0.3的向量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange( 0, 2, 0.3 ) # 接受浮点参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1.3 zeros\n",
    "\n",
    "`zeros`函数在我们要生成的向量所有元素都为0时使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  0.,  0.,  0.,  0.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = np.zeros(5)\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1.4 ones\n",
    "\n",
    "`ones`函数在向量中所有元素均为1时使用。让我们说，我们想要生成一个变量包含了的所有元素为1，并且维度为`3×2`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  1.],\n",
       "       [ 1.,  1.],\n",
       "       [ 1.,  1.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = np.ones((3,2))\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "记住，如果维度超过1，那么维度将以`tuple`的形式呈现。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1.5 empty\n",
    "\n",
    "`empty`在初始化变量时非常有用。这将无用的数据分配给稍后要修改的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  6.11020233e-313,   6.11020232e-313,   6.11020237e-313,\n",
       "          6.11020239e-313,   6.11020239e-313],\n",
       "       [  6.11020237e-313,   6.11020240e-313,   6.11020233e-313,\n",
       "          6.11020240e-313,   6.11020240e-313]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = np.empty((2,5))\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了`zeros`,`ones`,`empty`，数据类型（例如，`int`,`float`等）同样可以被定义。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1778384997,   56885249,     156928, 1509950083,   56950787],\n",
       "       [  40108288,   16798464, 1392509540, -978695392,         28]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = np.empty((2,5),int)\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "你可以看到`foo`中所有元素是整型，即便数值是无用的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1.6 rand\n",
    "\n",
    "`rand`用于生成0和1之间均匀分布的随机变量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.31926042,  0.88777405],\n",
       "       [ 0.03734307,  0.4816187 ],\n",
       "       [ 0.72920331,  0.85784028]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = np.random.rand(3,2)\n",
    "foo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### randn\n",
    "\n",
    "`randn`用来生成具有正态分布的随机变量，其均值为0，方差为1。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.87325251, -0.80658988,  0.93298161,  1.26130384],\n",
       "       [ 1.04079985,  1.41569764, -0.51769584,  0.12887116]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foo = np.random.randn(2,4)\n",
    "foo"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
